namespace CSharpPro2._1016;

public class Demo
{
    // 作业一
    public  void TestPrint1()
    {
        
            Console.Write("请输入第一个整数：");
            int x = int.Parse(Console.ReadLine());
            
            Console.Write("请输入第二个整数：");
            int y = int.Parse(Console.ReadLine());
            
            Console.Write("请输入第三个整数：");
            int z = int.Parse(Console.ReadLine());
            
            // 排序
            int[] numbers = { x, y, z };
            Array.Sort(numbers);
            
            Console.WriteLine($"排序结果：{numbers[0]} {numbers[1]} {numbers[2]}");
       
    }
    //作业二
    public  void TestPrint2()
    {
        int sum = 0;
        int count = 0;
        
        for (int i = 1; i <= 100; i++)
        {
            if (i % 7 != 0 && !i.ToString().Contains('7'))
            {
                Console.Write(i + "\t");
                sum += i;
                count++;
                
                if (count % 10 == 0)
                    Console.WriteLine();
            }
        }
        
        Console.WriteLine($"\n总和：{sum}");
    }
    //作业三
    public  void TestPrint3()
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}×{i}={i * j}\t");
            }
            Console.WriteLine();
        }
    }
    //作业四
    public  void TestPrint4()
    {
        for (int i = 2; i <= 1000; i++)
        {
            int sum = 0;
            for (int j = 1; j <= i / 2; j++)
            {
                if (i % j == 0)
                {
                    sum += j;
                }
            }
            
            if (sum == i)
            {
                Console.Write($"{i} = ");
                List<int> factors = new List<int>();
                for (int j = 1; j <= i / 2; j++)
                {
                    if (i % j == 0)
                    {
                        factors.Add(j);
                    }
                }
                Console.WriteLine(string.Join(" + ", factors));
            }
        }
    }
    //作业五
    public  void TestPrint5()
    {
        try
        {
            Console.Write("请输入一个正整数：");
            string input = Console.ReadLine();
            
            if (string.IsNullOrWhiteSpace(input))
            {
                Console.WriteLine("输入不能为空！");
                return;
            }
            
            if (!int.TryParse(input, out int number) || number <= 0)
            {
                Console.WriteLine("请输入有效的正整数！");
                return;
            }
            
            // 判断位数
            int digits = input.Length;
            Console.WriteLine($"它是{digits}位数");
            
            // 逆序输出
            char[] charArray = input.ToCharArray();
            Array.Reverse(charArray);
            string reversed = new string(charArray);
            Console.WriteLine($"逆序输出：{reversed}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误：{ex.Message}");
        }
    }
    //作业六
    public  void TestPrint6()
    {
        try
        {
            Console.Write("请输入菱形的行数（奇数）：");
            int rows = int.Parse(Console.ReadLine());
            
            if (rows % 2 == 0)
            {
                Console.WriteLine("请输入奇数！");
                return;
            }
            
            int n = rows / 2;
            
            // 上半部分
            for (int i = 0; i <= n; i++)
            {
                for (int j = 0; j < n - i; j++)
                    Console.Write(" ");
                for (int j = 0; j < 2 * i + 1; j++)
                    Console.Write("*");
                Console.WriteLine();
            }
            
            // 下半部分
            for (int i = n - 1; i >= 0; i--)
            {
                for (int j = 0; j < n - i; j++)
                    Console.Write(" ");
                for (int j = 0; j < 2 * i + 1; j++)
                    Console.Write("*");
                Console.WriteLine();
            }
        }
        catch (FormatException)
        {
            Console.WriteLine("输入格式错误，请输入整数！");
        }
    }
}